package com.baizhi.action;

import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@Scope("prototype")
public class UserAction {
    @Autowired
    private UserService userService;
    private String username;
    private String password;

    //登录
    public void login() throws Exception {
        User user = userService.login(username, password);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        Map<String, Object> map = new HashMap<>();
        if (user != null){
            map.put("status", "success");
        }else{
            map.put("stauts", "failed");
            map.put("msg", "账户或密码错误");
        }
        //将map转化为json
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
